package gui;

import gui.newQuiz.QuestionWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JOptionPane;

import quiz.SimpleQuiz;

public class ButtonActionListener implements ActionListener {
	GUIMain main;
	String quizNavn;
	File outPutFile;

	public ButtonActionListener(GUIMain main) {
		this.main = main;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(main.printBasicSetup)) {
			printBasicSetup();
		} else if (e.getSource().equals(main.saveQuiz)) {
			saveQuizErrorHandler();
		} else if (e.getSource().equals(main.loadQuiz)) {
			loadQuiz();
		} else if (e.getSource().equals(main.printHelper)) {
			printHelper();
		} else if (e.getSource().equals(main.printSolution)) {
			printSolution();
		} else if (e.getSource().equals(main.createQuiz)) {
			createQuiz();
		}
	}

	private void createQuiz() {
		createOrLoadFileForSavingHandler();
		main.createQuiz.setEnabled(false);
		main.saveQuiz.setEnabled(true);
		QuestionWindow qw = new QuestionWindow(main.quiz);
		qw.setVisible(true);
	}

	private void saveQuizErrorHandler() {
		try {
			saveQuiz();
		} catch (IOException i) {
			i.printStackTrace();
		}
	}

	private void saveQuiz() throws IOException {
		main.displayArea.setText("Saved!");
		ObjectOutputStream objectOutStream = getObjectOutPutstream(outPutFile);
		objectOutStream.writeObject(main.quiz);
	}

	private ObjectOutputStream getObjectOutPutstream(File outPutFile)
			throws IOException {
		return new ObjectOutputStream(new FileOutputStream(outPutFile));
	}

	private File createOrLoadFileForSavingHandler() {
		try {
			createOrLoadFileForSaving();
		} catch (IOException e) {
			System.out.println(e);
		}
		return outPutFile;
	}

	private void createOrLoadFileForSaving() throws IOException {
		String quizName = JOptionPane.showInputDialog(main,
				"Hva skal quizen hete?");
		outPutFile = new File("savedQuiz" + File.separatorChar + quizName
				+ ".quiz");
		if (!outPutFile.exists()) {
			outPutFile.createNewFile();
		}
	}

	private void loadQuiz() {
		try {
			Object[] quizzesFound = findAvailableSavedQuizzez();
			String quizName = (String) JOptionPane
					.showInputDialog(main, "Velg lagret quiz!",
							"Customized Dialog", JOptionPane.PLAIN_MESSAGE,
							null, quizzesFound, "wtflolrofl");

			File outPutFile = new File("savedQuiz" + File.separatorChar
					+ quizName);

			FileInputStream fis = new FileInputStream(outPutFile);
			ObjectInputStream ois = new ObjectInputStream(fis);
			main.quiz = (SimpleQuiz) ois.readObject();
			String quizSelected = displayAvailableQuizzezAndSelectOne();
			loadSelectedQuiz(quizSelected);
		} catch (FileNotFoundException f) {
			f.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		}
	}

	private Object[] findAvailableSavedQuizzez() {
		List quiz = new ArrayList();
		File folder = new File("savedQuiz");
		File[] files = folder.listFiles();
		Object[] quizNames = new Object[files.length];
		for (File file : files) {
			if (file.getName().endsWith(".quiz")) {
				quiz.add(file.getName());
			}
		}
		return quiz.toArray();
	}

	private String displayAvailableQuizzezAndSelectOne() {
		return null;
	}

	private void loadSelectedQuiz(String quizSelected) {

	}

	private void printBasicSetup() {
		main.displayArea.setText(main.quiz.basicSetupPrint());
	}

	private void printHelper() {
		main.displayArea.setText(main.quiz.printMasterHelper());
	}

	private void printSolution() {
		main.displayArea.setText(main.quiz.printMasterSolutions());
	}

}
